<?php
/* bossNewsSearch - interface for use with Yahoo search BOSS API news searches*/
require_once("yahooConfig.php");

class bossNewsSearch
{
        protected $url;            // Interface(URL) used to gain the data
        protected $xml;            // Raw XML
        protected $xmlObj;         // SimpleXML object
	protected $searchTerms;    //

     public function __construct($terms = NULL)
     {
	  $this->searchTerms = $terms;
     }

     public function __get($name)
     {
	  return $this->$name;
     }

     public function __set($attribute, $value)
     {
	  $this->$attribute = $value;
     }

     public function addTerm($term)
     {
	 // echo 'adding '.$term.'<br />';
	  $this->searchTerms .= $term."%20";
     }

     /* getWords - main function of class    */
     /* performs Yahoo News search for given */
     /* search terms.                        */
     /* Returns a string consisting of the   */
     /* titles and long abstracts of all of  */
     /* the results.                         */
     public function getWords()
     {
	  $this->search();

	  if(empty($this->xmlObj->resultset_news->result[0])){
		    throw new NoNewsResultsException();
	  }
	  //otherwise good to go
	  $result_string = NULL;

	  foreach($this->xmlObj->resultset_news->result as $res)
	  {
	       //echo 'result: '.$res->abstract. ' end.<br />';
	       $result_string .= $res->title .' ';
	       $result_string .= $res->abstract.' ';

	  }
	  return $result_string;
     }

     /* getKeyWordArray                       */
     /* performs Yahoo News search for given  */
     /* search terms.                         */
     /* Returns an array containing all words */
     /* found in the titles and long abstracts*/
     /* of the results except for those in the*/
     /* $stopArray parameter                  */
     /* Also excludes any numerical "words"   */
     public function getKeyWordArray($stopArray = array())
     {


	  if(empty($this->xmlObj->resultset_news->result[0])){
		    throw new NoNewsResultsException();
	  }
	  //otherwise good to go
	  $result_string = NULL;
	  $fixedResults = array();
	  $newsResults = array();

	  foreach($this->xmlObj->resultset_news->result as $res)
	  {
	       //echo 'result: '.$res->abstract. ' end.<br />';
	       $result_string .= $res->title .' ';
	       $result_string .= $res->abstract.' ';

	  }
	  //format the returned words for cloud
	  //remove all numbers and punctuation
	  $result_string = preg_replace('/[^a-zA-Z]/',' ', $result_string);
	  $result_string = strtolower($result_string);
	  //put individual words into an array
	  $newsResults = explode(" ", $result_string);
	  //remove all whitespaces
	  //for some reason cannot save this back to same array
	  $fixedResults = array_filter($newsResults,'trim');
	  //take out the stop words before putting into cloud
	  $fixedResults = array_diff($fixedResults, $stopArray);


// 	  foreach ($fixedResults as $res)
// 	  {
// 	       echo 'Word:'.$res.':<br />';
// 	  }
	  return $fixedResults;
     }

     public function search()
     {
	  if(empty($this->searchTerms)){
	       throw new NeedNewsTermsException();
	  }

	  if (!$this->xml = file_get_contents(BOSSNEWSURL.$this->searchTerms.BOSSNEWSARGS)) {
	       throw new NoNewsResultsException();
	  }
	  //all is good make simple XML object
	  $this->xmlObj = new SimpleXMLElement($this->xml, LIBXML_NOCDATA);

     }

}//end bossNewsSearch class




class NeedNewsTermsException extends Exception
{
     function _toString()
     {
	  return 'NeedNewsTermsException '. $this->getCode(). ': '. $this->getMessage().'<br /> in '. $this->getFile(). ' on line '.$this->getLine(). '<br />';
     }
}

class NoNewsResultsException extends Exception
{
     function _toString()
     {
	  return 'NoNewsResultsException'. $this->getCode(). ': '. $this->getMessage().'<br /> in '. $this->getFile(). ' on line '.$this->getLine(). '<br />';
     }
}


?>